package com.product.magina.wechatserver.service.impl;

import com.google.common.collect.Lists;
import com.product.magina.wechatserver.dao.DsmtDistributionPlanDao;
import com.product.magina.wechatserver.dao.DsmtPlanCustomerDao;
import com.product.magina.wechatserver.entity.DsmtDistributionPlan;
import com.product.magina.wechatserver.entity.DsmtPlanCustomer;
import com.product.magina.wechatserver.service.DsmtDistributionPlanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Transactional
@Service(value = "dsmtDistributionPlanService")
@Slf4j
public class DsmtDistributionPlanServiceImpl implements DsmtDistributionPlanService {
    @Autowired
    DsmtDistributionPlanDao dsmtDistributionPlanDao;
    @Autowired
    DsmtPlanCustomerDao dsmtPlanCustomerDao;

    @Override
    public List<DsmtDistributionPlan> selectPlanByCustomer(String code) {
        //暂时不考虑代理的客户
        List planList = Lists.newArrayList();
        List<DsmtPlanCustomer> planCustomerList = dsmtPlanCustomerDao.getDsmtPlanByCustomerCode(code);
        if(!CollectionUtils.isEmpty(planCustomerList)){
            List<Integer> planIdList = planCustomerList.stream().map(p -> p.getDistributionPlanId()).distinct().collect(Collectors.toList());
            if(!CollectionUtils.isEmpty(planIdList)){
                planIdList.stream().forEach(p -> {
                    DsmtDistributionPlan plan = dsmtDistributionPlanDao.getDistributionById(p);
                    planList.add(plan);
                });
            }
        }
        return planList;
    }

}
